import HRDCCustomerApi from '@/api/HRDCCustomer.api.js'
import dataApi from '@/api/data.js'
import visibvleTreeList from '../HRDC-common/visibleTreeList.vue'
export default {
  data () {
    return {
      dolderNames: '',
      salesIds: [],
      treeData: [],

      customerId: '', // 客户id
      contact_arr: [], // 联系人列表
      customerBusinesstype_arr: [], // 行业
      sizeArr: [], // 公司规模
      characterArr: [], // 企业性质
      cooperateType_arr: [], // 合作类型
      communicationtype: [], // 沟通方式
      communicationTotal: 0,
      communicationList: [] // 沟通记录列表
    }
  },
  components: {
    visibvleTreeList
  },
  computed: {
    tipIsShow () {
      if (this.dolderNames) {
        return true
      } else {
        return false
      }
    }
  },
  created () {
    this.getEnum()
    this.getVisibleTreeList()
  },
  methods: {
    // 跳往详情
    gotoDetail (id) {
      console.log(id)
      this.$router.push({ name: 'h-c-detail', query: { id: id } })
    },
    // 设置表头
    getTableColumns (arr) {
      const tableColumns = {
        selection: {
          type: 'selection',
          width: 60,
          align: 'center',
          fixed: 'left'
        },
        code: {
          title: '需求编号',
          minWidth: 150,
          align: 'center',
          key: 'code'
        },
        requirementCode: {
          title: '需求编号',
          minWidth: 150,
          align: 'center',
          key: 'requirementCode'
        },
        createTime: {
          title: '新建日期',
          minWidth: 180,
          align: 'center',
          render: (h, params) => {
            let row = params.row
            let times = row.createTime.slice(0, 19).replace(/T/g, ' ')
            return h('span', times)
          }
        },
        brief: {
          title: '客户需求',
          minWidth: 150,
          tooltip: true,
          align: 'center',
          key: 'requirementTypeName'
        },
        customerName: {
          title: '客户名称',
          minWidth: 150,
          align: 'center',
          slot: 'customerName'
        },
        characterName: {
          title: '企业性质',
          minWidth: 100,
          tooltip: true,
          align: 'center',
          key: 'characterName'
        },
        businessTypeName: {
          title: '所属行业',
          minWidth: 120,
          tooltip: true,
          align: 'center',
          key: 'businessTypeName'
        },
        size: {
          title: '企业规模',
          minWidth: 140,
          align: 'center',
          render: (h, params) => {
            let row = params.row
            let str = ''
            let arr = this.sizeArr
            let text = arr.find((item) => {
              return item.key === row.size.toString()
            })
            if (text) {
              str = text.value
            } else {
              str = '未填写'
            }
            return h('span', str)
          }

        },
        registerCapital: {
          title: '注册资金',
          minWidth: 120,
          tooltip: true,
          align: 'center',
          key: 'registerCapital'
        },

        planStartTime: {
          title: '预计启动时间',
          minWidth: 150,
          align: 'center',
          render: (h, params) => {
            let row = params.row
            let times = row.planStartTime.slice(0, 10).replace(/T/g, '')
            return h('span', times)
          }
        },
        budget: {
          title: '需求预算金额',
          minWidth: 150,
          align: 'center',
          key: 'budget'
        },
        planName: {
          title: '方案名称',
          minWidth: 100,
          tooltip: true,
          align: 'center',
          key: 'name'
        },
        planFile: {
          title: '方案附件',
          minWidth: 95,
          align: 'center',
          slot: 'planFile',
          fixed: 'right'
        },
        policymaker: {
          title: '决策人',
          minWidth: 100,
          align: 'center',
          key: 'policymaker'
        },
        policyEffector: {
          title: '决策影响人',
          minWidth: 110,
          align: 'center',
          key: 'policyEffector'
        },
        purchaseFlow: {
          title: '采购流程',
          minWidth: 100,
          tooltip: true,
          align: 'center',
          key: 'purchaseFlow'
        },
        hz_price: {
          title: '预期合作价格',
          minWidth: 130,
          align: 'center',
          key: 'price'
        },
        hz_content: {
          title: '合作内容',
          minWidth: 100,
          align: 'center',
          key: 'content'
        },
        hz_challenge: {
          title: '谈判挑战',
          minWidth: 100,
          align: 'center',
          key: 'challenge'
        },
        ht_code: {
          title: '合同编号',
          minWidth: 120,
          align: 'center',
          key: 'code'
        },
        ht_signTime: {
          title: '签约日期',
          minWidth: 130,
          align: 'center',
          render: (h, params) => {
            let row = params.row
            let times = row.signTime.slice(0, 10).replace(/T/g, '')
            return h('span', times)
          }
        },
        ht_amt: {
          title: '签约金额',
          minWidth: 100,
          align: 'center',
          key: 'amt'
        },
        ht_fujian: {
          title: '合同附件',
          minWidth: 120,
          align: 'center',
          slot: 'ht_fujian',
          fixed: 'right'
        },

        webSite: {
          title: '客户网站',
          minWidth: 150,
          align: 'center',
          key: 'webSite'
        },
        phone: {
          title: '客户公司电话',
          minWidth: 130,
          align: 'center',
          key: 'phone'
        },
        createrName: {
          title: '客户所属',
          minWidth: 100,
          align: 'center',
          key: 'createrName'
        },
        status: {
          title: '状态',
          minWidth: 150,
          align: 'center',
          key: 'status'
        },
        closedReason: {
          title: '关闭原因',
          minWidth: 150,
          align: 'center',
          key: 'closedReason'
        },
        closedByName: {
          title: '关闭人',
          minWidth: 150,
          align: 'center',
          key: 'closedByName'
        },
        // xinjianshijian: {
        //   title: '新建时间',
        //   minWidth: 150,
        //   align: 'center',
        //   key: 'xinjianshijian'
        // },
        diagnose: {
          title: '2D诊断表',
          minWidth: 100,
          align: 'center',
          slot: 'diagnose',
          fixed: 'right'
        },
        communicate: {
          title: '沟通记录',
          minWidth: 170,
          align: 'center',
          slot: 'communicate',
          fixed: 'right'
        },
        action: {
          title: '操作',
          minWidth: 200,
          align: 'center',
          slot: 'action',
          fixed: 'right'
        }
      }
      let data = []

      arr.forEach(col => data.push(tableColumns[col]))

      return data
    },

    // 获取当前角色可见的销售人员
    getVisibleTreeList () {
      HRDCCustomerApi.GetVisibleTreeList().then(res => {
        console.log(res)
        this.treeData = res.data.data
      })
    },
    chooseSaler () {
      this.$refs.treeModal.show()
    },
    salersClear () {
      console.log(22222)
      this.salesIds = []
      this.getVisibleTreeList()
    },
    getSalerId (val) {
      console.log(val)
      let idArr = []
      let nameArr = []
      val.forEach(item => {
        if (item.type === "4") {
          idArr.push(item.id)
          nameArr.push(item.title)
        }
      })
      this.salesIds = idArr
      this.dolderNames = nameArr.join(',')
    },

    // 添加沟通记录
    addCommunication (id) {
      this.customerId = id
      this.getContactList()
      this.$refs.createCommunication.creatOpen()
    },
    checkCommunication (id) {
      this.customerId = id
      this.getCommunicationList()
      this.$refs.checkCommunication.creatOpen()
    },
    // 获取联系人列表
    getContactList () {
      HRDCCustomerApi.ContacterList({
        customerId: this.customerId,
        PageIndex: 0,
        PageSize: 100
      }).then(res => {
        if (res.data.success) {
          this.contact_arr = res.data.data
          // this.$refs.createCommunication.creatOpen()
        } else {
          
          this.$Message.error({
            content: res.data.message,
            duration: 10,
            closable: true
          })
        }
      })
    },
    // 获取沟通记录列表
    getCommunicationList () {
      HRDCCustomerApi.CommunicationList({
        customerId: this.customerId,
        PageIndex: 0,
        PageSize: 100
      }).then(res => {
        if (res.data.success) {
          this.communicationList = res.data.data
          this.communicationTotal = res.data.totalRows
        } else {
          
          this.$Message.error({
            content: res.data.message,
            duration: 10,
            closable: true
          })
        }
      })
    },
    // 关闭需求
    closeRequirement (id) {
      this.$Modal.confirm({
        title: '确定关闭吗？',
        onOk: () => {
          HRDCCustomerApi.CloseRequirement({
            id: id
          }).then(res => {
            if (res.data.success) {
              this.$Message.success(res.data.message)
              this.getList()
            } else {
              
              this.$Message.error({
                content: res.data.message,
                duration: 10,
                closable: true
              })
            }
          })
        },
        onCancel: () => {
        }
      });
    },
    getEnum () {
      let groupNames = 'customer.businesstype, sales.communicationtype, customer.size, customer.character, customer.type'
      dataApi.getEnum({
        groupName: groupNames
      }).then((res) => {
        this.customerBusinesstype_arr = this.getItems(res.data, 'customer.businesstype')
        this.communicationtype = this.getItems(res.data, 'sales.communicationtype')
        this.sizeArr = this.getItems(res.data, 'customer.size')
        this.characterArr = this.getItems(res.data, 'customer.character')
        this.cooperateType_arr = this.getItems(res.data, 'customer.type')
      })
      // Common.getEnum(groupName, this.arrListDown)
    },
    getItems (arr, key) {
      let object = arr.find((item) => {
        return item.group === key
      })
      return object.items
    }
  }
}
